import {
  getFundingRecordTable
} from '../../api/financial'
import {
  FUNDING_RECORD_LIST,
  FUNDING_RECORD_CURRENT_ROW,
  FUNDING_RECORD_DATA_COUNT,
  FUNDING_RECORD_HISTORY_DATA,
  FUNDING_RECORD_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    fundingRecordList: [],
    fundingRecordDataCount: null,
    fundingRecordPageSize: 20,
    fundingRecordHistoryData: [],
    row: {
      company: '',
      tel: '',
      type: '',
      state: '',
      price: '',
      ctime: '',
      des: '',
      paytype: ''
    }
  },
  getters: {
    fundingRecordList: state => state.fundingRecordList
  },
  mutations: {
    [FUNDING_RECORD_LIST] (state, data) {
      state.fundingRecordList = data
    },
    [FUNDING_RECORD_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [FUNDING_RECORD_DATA_COUNT] (state, data) {
      state.fundingRecordDataCount = data
    },
    [FUNDING_RECORD_HISTORY_DATA] (state, data) {
      state.fundingRecordHistoryData = data
    },
    [FUNDING_RECORD_PAGE_SIZE] (state, data) {
      state.fundingRecordPageSize = data
    }
  },
  actions: {
    async getFundingRecordList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getFundingRecordTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          company: item.company,
          tel: item.tel,
          type: item.type,
          state: item.state,
          price: item.price,
          ctime: item.ctime,
          des: item.des,
          paytype: item.paytype
        }]
      })
      commit(FUNDING_RECORD_LIST, newData)
      commit(FUNDING_RECORD_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.fundingRecord.fundingRecordPageSize) {
        commit(FUNDING_RECORD_HISTORY_DATA, newData)
      } else {
        commit(FUNDING_RECORD_HISTORY_DATA, newData.slice(0, rootState.fundingRecord.fundingRecordPageSize))
      }
      return resp
    },
    // async editFundingMessageData ({ commit, rootState }, playload) {
    //   let data = {}
    //   if (Object.keys(playload).length > 0) {
    //     data = playload
    //   }
    //   const resp = await editAccountRecord(data)
    //   return resp
    // },
    async getFundingRecordCurrentRow ({ commit, rootState }, row) {
      commit(FUNDING_RECORD_CURRENT_ROW, row)
    },
    async changeFundingRecordPage ({ commit, rootState }, index) {
      let data = rootState.fundingRecord.fundingRecordList
      let _start = (index - 1) * rootState.fundingRecord.fundingRecordPageSize
      let _end = index * rootState.fundingRecord.fundingRecordPageSize
      commit(FUNDING_RECORD_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeFundingRecordPageSize ({ commit, rootState }, value) {
      commit(FUNDING_RECORD_PAGE_SIZE, value)
    }
  }
}
